﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;
using System.Web.Mvc;

namespace AnalisisDeRiesgos.Models.User
{
    public class EditUserModel
    {
        [DisplayName("CI")]       
        public string CI { get; set; }

        [DisplayName("Nombre(s)")]
        [Required(ErrorMessage = "El nombre es requerido")]
        [StringLength(50, ErrorMessage = "El nombre debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z ñÑáéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras mayusculas o minusculas")]
        public String FirstName { set; get; } // The First Name of the user

        [DisplayName("Apellido Paterno")]
        [Required(ErrorMessage = "El apellido paterno es requerido")]
        [StringLength(50, ErrorMessage = "El apellido paterno debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z ñÑáéíóúÁÉÍÓÚ]+$", ErrorMessage = "El apellido paterno debe ser solo letras mayusculas o minusculas")]
        public String LastName { set; get; } // The Last name of the user

        [DisplayName("Apellido Materno")]
        [Required(ErrorMessage = "El apellido materno es requerido")]
        [StringLength(50, ErrorMessage = "El apellido materno debe ser máximo de 50 caracteres")]
        [RegularExpression(@"^[a-zA-Z ñÑáéíóúÁÉÍÓÚ]+$", ErrorMessage = "El apellido materno debe ser solo letras mayusculas o minusculas")]
        public String SecondLastName { set; get; } // The second last name of the user

        [DisplayName("Privilegios")]
        [Required(ErrorMessage = "El privilegio es requerido")]
        public int Privilege { set; get; }

        public IEnumerable<SelectListItem> PrivilegeList
        {
            get
            {
                return privilegeList;
            }
        }

        private List<SelectListItem> privilegeList;

        private riskanalysisEntities entities = new riskanalysisEntities(); // A reference to Entities

        public EditUserModel()
        {
            privilegeList = new List<SelectListItem>();

            privilegeList.Add(new SelectListItem
            {
                Text = "Administrador",
                Value = Convert.ToInt32(Privileges.Administrator).ToString()
            });

            privilegeList.Add(new SelectListItem
            {
                Text = "Gerente General de Seguridad",
                Value = Convert.ToInt32(Privileges.GeneralSecurity).ToString(),
            });

            privilegeList.Add(new SelectListItem
            {
                Text = "Encargado de Seguridad de Area",
                Value = Convert.ToInt32(Privileges.AreaSecurity).ToString(),
            });

            privilegeList.Add(new SelectListItem
            {
                Text = "Empleado",
                Value = Convert.ToInt32(Privileges.Employe).ToString(),
            });
        }

        public void FillUserById(int theCI)
        {
            var userList = from u in entities.users
                           where u.CI == theCI && u.enabled
                           select u;

            if (userList.Count() <= 0)
                throw new EditUserException("El usuario especificado no fue encontrado");

            user theUser = userList.First();

            CI = theUser.CI.ToString();
            FirstName = theUser.firstName;
            LastName = theUser.lastName;
            SecondLastName = theUser.secondLastName;
            Privilege = theUser.privilege;
        }

        public void Save()
        {
            int theCI = Convert.ToInt32(CI);
            var userList = from u in entities.users
                           where u.CI == theCI && u.enabled
                           select u;

            if (userList.Count() <= 0)
                throw new EditUserException("El usuario especificado no fue encontrado");

            user theUser = userList.First();

            theUser.CI = Convert.ToInt32(CI);
            theUser.firstName = FirstName;
            theUser.lastName = LastName;
            theUser.secondLastName = SecondLastName;
            theUser.privilege = Privilege;
            
            entities.SaveChanges();
        }
    }
}